﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.SM;
using dz.u9.webapi.u9schema;
using dz.util.core;
using Microsoft.AspNetCore.Mvc;

namespace dz.u9.webapi.Controllers
{
    /// <summary>
    /// 客户价表
    /// </summary>

    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CustPriceController : U9BaseController
    {
        [HttpPost]
        public async Task<U9CResult> Add(CustPrice dto)
        {

            if (IsFirst(dto))
            {
                return await U9Invoke(JsonUtil.Serialize(GetPriceListDto(dto)), new ApiParm() { Url = "DzSalePriceList/Add", Desc = "销售价表新增" });
            }
            else
            {
                return await U9Invoke(JsonUtil.Serialize(GetPriceAdjust(dto)), new ApiParm() { Url = "DzSalePriceAdjust/AddAndAudit", Desc = "销售价格调整新增审核" });                
            }   
        }

        private DzSalePriceAdjustmentDTO GetPriceAdjust(CustPrice dto)
        {
            DzSalePriceAdjustmentDTO rtn = new DzSalePriceAdjustmentDTO();

            rtn.DocNo = "aaa";
            rtn.PriceList = GetPriceList(dto);
            rtn.DocumentType = "SDTJ0001";
            rtn.Org = dto.Org;

            List<DzSalePriceAdjustLineDTO> lines = new List<DzSalePriceAdjustLineDTO>();

            foreach (var item in dto.CustPriceLines)
            {
                DzSalePriceAdjustLineDTO line = new DzSalePriceAdjustLineDTO();

                line.ItemInfo = item.ItemInfo;
                line.Uom = item.Uom;
                line.FromDate = DateTime.Now.Date;
                line.NewPrice = item.Price;
                line.DescFlexField = item.DescFlexField;
                line.Customer = dto.Code;

                lines.Add(line);
            }

            rtn.SalePriceAdjustLines = lines;

            return rtn;
        }

        private string GetPriceList(CustPrice dto)
        {
            string sql = @$"select top 1 b.code from SPR_SalePriceLine a
                            inner join SPR_SalePriceList b on a.SalePriceList = b.id
                            inner join CBO_Customer c on a.Customer = c.id
                            inner join Base_Organization d on d.ID = b.Org
                            where c.Code='{dto.Code}' and d.code = '{dto.Org}'";

            return utils.ExecuteScalar<string>(sql).Result;
            
        }

        private DzSalePriceListDTO GetPriceListDto(CustPrice dto)
        {
            DzSalePriceListDTO rtn = new DzSalePriceListDTO();

            rtn.Code = dto.Code;
            rtn.Currency = "C001";

            List<DzSalePriceLineDTO> lines = new List<DzSalePriceLineDTO>();

            foreach (var item in dto.CustPriceLines)
            {
                DzSalePriceLineDTO line = new DzSalePriceLineDTO();

                line.Price = item.Price;
                line.Customer = dto.Code;
                line.Uom = item.Uom;
                line.ItemInfo = item.ItemInfo;
                line.DescFlexField = item.DescFlexField;
                line.FromDate = DateTime.Now.Date;
                lines.Add(line);
            }
            

            rtn.SalePriceLines = lines;

            return rtn;
        }        

        private bool IsFirst(CustPrice dto)
        {
            string sql = @$"select count(1) from SPR_SalePriceLine a
                            inner join SPR_SalePriceList b on a.SalePriceList = b.id
                            inner join CBO_Customer c on a.Customer = c.id
                            inner join Base_Organization d on d.ID = b.Org
                            where c.Code='{dto.Code}' and d.code = '{dto.Org}'";

            int r =  utils.ExecuteScalar<int>(sql).Result;

            return r == 0;
        }
    }
}
